package com.sxbang.bms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sxbang.bms.base.result.Results;
import com.sxbang.bms.config.jwt.JwtUtils;
import com.sxbang.bms.model.SysPermission;
import com.sxbang.bms.model.SysUser;
import com.sxbang.bms.service.LoginService;
import com.sxbang.bms.service.PermissionService;
import com.sxbang.bms.service.UserService;
import com.sxbang.bms.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Permission;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    public UserService userService;
    @Autowired
    public PermissionService permiService;

    @Override
    public Results<JSONObject> getUerByUsernameAndPassword(String username, String password) {
        JSONObject jsonObject = new JSONObject();
        MD5 md5 = new MD5();
        String token= null;
        if(null != password){
            String passwordMd5 =  md5.crypt(password);
            SysUser user = userService.getUerByUsernameAndPassword(username,passwordMd5);
            if(null != user){
                token= JwtUtils.getToken(user.getUsername());
                Results<SysPermission> permissionData = permiService.listByUserId(user.getId());
                jsonObject.put("token",token);
                jsonObject.put("user",user);
                jsonObject.put("permission",permissionData);
                jsonObject.put("loginStatusCode",1);
            }else{
                jsonObject.put("loginStatusCode",0);
            }
        }else{
            jsonObject.put("loginStatusCode",0);
        }
        return Results.success(jsonObject);
    }
}
